package jianger.v2;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import jianger.pojo.Product;
import jianger.utils.GetDocumentUtils;
import jianger.utils.ProductDao;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.nodes.Document;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author jianger
 * @date 2018/2/27
 * slave主要是从redis中获取pid，然后解析页面信息，存到数据库中
 * 技术点：
 * 1. redis
 * 2. 多线程
 */

public class Slave {

    //创建一个jedis池
    private static GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    private static JedisPool jedisPool = new JedisPool(poolConfig, "127.0.0.1", 6379);
    private static ExecutorService pool = Executors.newFixedThreadPool(30);
    //创建线程

    private static ProductDao productDao = new ProductDao();

    public static void main(String[] args) {

        getProduct();

    }

    private static void getProduct() {

        for (int i = 0; i < 28; i++) {
            pool.execute(new Runnable() {
                public void run() {
                    Jedis jedis = jedisPool.getResource();
                    while (true) {
                        List<String> pids = jedis.blpop(0, "pid");
                        String pid = pids.get(1);
                        // 详情页的url  https://item.jd.com/5025971.html
                        String detailUrl = "https://item.jd.com/" + pid + ".html"; // 拼接手机详情页的url

                        // 访问每个手机的详情页
                        Document dateil = null;
                        try {
                            dateil = GetDocumentUtils.getDecument(detailUrl);

                            String phoneName = dateil.select(".sku-name").text(); //解析出手机的名字
                            //因为价格是ajax异步加载的，所以要获取价格需要一部的解析价格的url

                            String pirceUrl = "https://p.3.cn/prices/mgets?pduid=2018573997&skuIds=J_" + pid;
                            // 1. 获取一个httpGet对象
                            HttpGet httpGet = new HttpGet(pirceUrl);
                            // 2.获取一个httpclient对象，用来执行http请求
                            CloseableHttpClient httpclient = HttpClients.createDefault();
                            // 3.执行请求，并得到返回对象
                            CloseableHttpResponse response = httpclient.execute(httpGet);
                            // 4. 返回一个html
                            String html = EntityUtils.toString(response.getEntity(), Charset.forName("utf-8"));
                            Gson gson = new Gson();
                            @SuppressWarnings("unchecked")
                            List<LinkedTreeMap<String, String>> list = (List<LinkedTreeMap<String, String>>) gson.fromJson(html, List.class);
                            //获取list第一个元素
                            LinkedTreeMap<String, String> map = list.get(0);
                            String price = map.get("p");
                            //每次循环都创建一个product对象
                            Product product = new Product(pid, phoneName, detailUrl, price);
                            productDao.save(product);
                            System.out.println("哈哈，又来一个");
                        } catch (IOException e) {
                            e.printStackTrace();
                            System.out.println("哎，出错了");
                        }
                    }


                }
            });


        }


    }
}
